﻿using System.Collections.Generic;
using System.Web;
using OpenWaves.Pagination;

namespace OpenWaves.Web.Pagination
{
    public class PaginationController
    {
        private readonly PaginationUrlFactory urlFactory;

        public PaginationController(PaginationUrlFactory urlFactory)
        {
            this.urlFactory = urlFactory;

            this.ItemsPerPage = 10;
            this.MaxNumberOfPagesToShow = 5;
        }

        public PaginationController(HttpRequest request)
            : this(new PaginationUrlFactory(request))
        {
        }

        public PaginationViewModel<T> Paginate<T>(IEnumerable<T> items)
        {
            int totalItemsCount;
            var pages = items.Paginate(this.ItemsPerPage, out totalItemsCount);

            return PaginationViewModel.Create(
                totalItemsCount, 
                pages,
                this.urlFactory.CurrentPageIndex,
                this.MaxNumberOfPagesToShow);
        }

        public int ItemsPerPage { get; set; }

        public int MaxNumberOfPagesToShow { get; set; }

        public Url GetPageUrl(PageViewModel page)
        {
            return this.urlFactory.GetPageUrl(page);
        }
    }
}